package com.example.flutter_card_plugin.utils;

import android.content.Context;
import android.widget.Toast;

import java.io.IOException;
import java.security.InvalidParameterException;

/**
 * @项目名称:Card
 * @作者: zhongfei
 * @创建时间:2019\1\21
 * @描述: 打卡
 */
public class PunchClock {



    //设置自动发送延时
    public static void SetiDelayTime(SerialHelper ComPort, String sTime) {
        ComPort.setiDelay(Integer.parseInt(sTime));
    }

    //设置自动发送数据
    public static void SetLoopData(SerialHelper ComPort, String sLoopData) {
        ComPort.setTxtLoopData(sLoopData);
    }


    //打开串口
    public static void OpenComPort(Context mContext, SerialHelper ComPort) {
        try {
            ComPort.open();
        } catch (SecurityException e) {
            ShowMessage(mContext, "打开串口失败:没有串口读/写权限!");
        } catch (IOException e) {
            ShowMessage(mContext, "打开串口失败:未知错误!");
        } catch (InvalidParameterException e) {
            ShowMessage(mContext, "打开串口失败:参数错误!");
        }
    }

    //串口发送
    public static void sendPortData(SerialHelper ComPort, String sOut) {
        if (ComPort != null && ComPort.isOpen()) {
            ComPort.sendTxt(sOut);
        }
    }

    //设置自动发送模式开关
    public static void SetAutoSend(SerialHelper ComPort, boolean isAutoSend) {
        if (isAutoSend) {
            ComPort.startSend();
        } else {
            ComPort.stopSend();
        }
    }



    //关闭串口
    public static void CloseComPort(SerialHelper ComPort) {
        if (ComPort != null) {
            ComPort.stopSend();
            ComPort.close();
        }
    }

    public static void ShowMessage(Context mContext, String sMsg) {
        Toast.makeText(mContext,sMsg,Toast.LENGTH_SHORT).show();
    }




}
